# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv
from pymongo import MongoClient
from utils.HttpUtils import HttpUtils


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
233网校
https://www.233.com/
账号：15622288121
密码：qq674401454
aa1113211600020
o49bvhp5

"""


class Contents():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        self.cookie = cookiejar.CookieJar()
        # ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            # 'User-Agent': ua.random,
            'Accept': '*/*',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive',
            'Authorization': 'bearer hAtg1SRms891DHKgc-n8VNUadR9XEF4OpgkJZJuGvcyQ7G4GZs6nWbcxpeQwBrx4NB7OYAnwHQPp7-XvSZnWcBobaZzFAFTfdd7pIAJwTRspehwieogCmJ93tG46-jM8FcxaY01CZrsHjLyjXrbG5QPilR1wys2fB2u0VIWKvx7gRB6AngHzGEeIrZvvLTT_V09JrxkurImCR6AQGYvHdQ',
            'Cookie': 'gr_user_id=784d1bbc-1844-40cc-8177-e8411292dfec; 9212495b3b2ffbcd_gr_session_id=5eadf396-7472-405d-8ebe-c8c42e45e9b3; UM_distinctid=173b314a89f81f-0309e376cce3f6-3323765-144000-173b314a8a1ac7; 9212495b3b2ffbcd_gr_session_id_5eadf396-7472-405d-8ebe-c8c42e45e9b3=true; grwng_uid=9dfda592-0599-407c-9a26-a05885446607; Hm_lvt_41458584ac7611d4bad1fc47207293a2=1596437343; 233ZhichiUUID=1958be06-1280-47da-bda2-6465e919075c; tanchuan=1; acw_tc=2760820715964373446907969e8c8a462febc59791dff06af6cf2d2e8c7d57; ASP.NET_SessionId=jbur032k3l1balxfeo5awzzi; logRegphotoCode=1eff7cd721a07ad2eb24d8693946fd59; Auth=1ECD9843F731151AFD9D485AF3EA74D9CD1332D97C0B8CA2F982751CFFF768962952B4E2CCF0BA66BC8716706C775E147D5CB0676EB80776; AuthGuid=2DFE7E84A6FB730F2523AB23D6523171CADC056BBA2EE187B4701E7256AA0C44ED80FE0FBD12ED14; wxuserinforset=username=aa1113211600020&userID=29720770&headpic=http%3A//img.233.com/wx/img/uc/Avatar.png&nickname=233%u7F51%u6821%u7F51%u53CB&fsclassId=365&ksclassId=30&classname=%u4E2D%u7EA7%u4F1A%u8BA1%u5E08&registertime=2020/5/13 16:35:25&isnewuser=0&addtime=2020-08-03 14:49:54&paymoney=0; clientauthentication=eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyOTcyMDc3MCIsIm5pY2tuYW1lIjoiMjMz572R5qCh572R5Y-LIiwidXNlclR5cGUiOjEsInVzZXJOYW1lIjoiYWExMTEzMjExNjAwMDIwIiwiZXhwIjoxNTk2NDgwNTk0LCJ1c2VySWQiOiIyOTcyMDc3MCIsImlhdCI6MTU5NjQzNzM5NCwianRpIjoiNmM0NWYzMGMtMDRjZC00MmYzLTkyNzgtZmFlODQ4NmMxYTQ0In0.xfauPh5OjzUDuWD8ADTnXS0bnjHI4F0ypc87y9mDzq0; 9212495b3b2ffbcd_gr_last_sent_cs1=29720770; 9212495b3b2ffbcd_gr_last_sent_sid_with_cs1=5eadf396-7472-405d-8ebe-c8c42e45e9b3; Hm_lpvt_41458584ac7611d4bad1fc47207293a2=1596437395; fclasscname=100; userDefaultClassId=9; i_defaultpage=9; CNZZDATA1257783334=1592673571-1596434708-https%253A%252F%252Fwww.233.com%252F%7C1596434708; CNZZDATA4109769=cnzz_eid%3D1448553306-1596432451-https%253A%252F%252Fwww.233.com%252F%26ntime%3D1596432451; CNZZDATA4556415=cnzz_eid%3D1496858596-1596432561-https%253A%252F%252Fwx.233.com%252F%26ntime%3D1596432561; CNZZDATA3583776=cnzz_eid%3D1232141778-1596434754-https%253A%252F%252Fwx.233.com%252F%26ntime%3D1596434754; CNZZDATA1305401=cnzz_eid%3D282328896-1596435870-https%253A%252F%252Fwx.233.com%252F%26ntime%3D1596435870; CNZZDATA1257759894=1253891300-1596432442-https%253A%252F%252Fwx.233.com%252F%7C1596432442; _cnzz_CV1257759894=%E8%BA%AB%E4%BB%BD%7C%E6%B8%B8%E5%AE%A2%7C0; refererPageHref=prevPageHref%3Dhttps%3A//wx.233.com/tiku/exam/item/391611%26currentPageHref%3Dhttps%3A//wx.233.com/tiku/chapter/do/a1eb98b8fd9aeb6149c90e72ae4deb0b%3FfromUrl%3Dhttps%253A%252F%252Fwx.233.com%252Ftiku%252Fpaper%252Fyati%252F1197; 9212495b3b2ffbcd_gr_cs1=29720770; SERVERID=84819e1e04dd99f040e4a38f9dac16db|1596437686|1596437344',
            'DNT': '1',
            'Host': 'wx.233.com',
            'Referer': 'https://wx.233.com/tiku/chapter/do/e4c63110726d5d77d148c3dd34b0633c?isAutoDelRight=0&extractType=0&fromUrl=http%3A%2F%2Fwx.233.com%2Ftiku%2Fchapter',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
            'X-Requested-With': 'XMLHttpRequest'
        }


    def get_part(self, cursor, md5, tablename, type, title, part, part_class):
        postData = {
            'md5': md5,
            'type': '104',
            'pageIndex': cursor,
            'pageSize': '20',
            'isError': '0',
            '_': int(time.time() * 1000)

        }
        url = f"https://wx.233.com/tiku/exam/GetExam"
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        data_json = json.loads(html.text)
        count = data_json['list']['count']  # 总题数
        page = len(data_json['list']['listPageMap'])  # 总页数
        for d in data_json['list']['examDtoList']:
            dict_data = dict()
            dict_data['type'] = type  # 类别
            dict_data['title'] = title  # 科目
            dict_data['part'] = part  # 章
            dict_data['part_class'] = part_class  # 节
            dict_data['examId'] = d['examId']  # id
            dict_data['examTypeName'] = d['examTypeName']  # 题型
            dict_data['content'] = d['content']  # 题干
            dict_data['returnQuestion'] = d['questionArr']['returnQuestion']  # 问题
            dict_data['analysis'] = d['analysis']  # 分析
            dict_data['answer'] = d['answer']  # 正确答案选项
            if d['examTypeName'] == "判断题":
                if d['answer'] == "0":
                    dict_data['answer'] = "错误"
                elif d['answer'] == "1":
                    dict_data['answer'] = "正确"
                else:
                    dict_data['answer'] = ""

            # 选项
            tag_count = 1
            for t in d['optionList']:
                dict_data['option_' + str(tag_count)] = t.replace("&nbsp;", "")
                tag_count += 1
            # 章+节
            if len(d['knowledge']) > 0:
                parentChapterName = d['knowledge'][0]['parentChapterName']  # 章
                chapterName = d['knowledge'][0]['chapterName']  # 节
                knowLedgeName = d['knowledge'][0]['knowLedgeName']  # 节
                yaoQiuName = d['knowledge'][0]['yaoQiuName']  # 节
                pageNum = d['knowledge'][0]['pageNum']  # 教材页码
                dict_data['zsd'] = parentChapterName + ">" + chapterName + ">" + knowLedgeName + ">" + "(" + yaoQiuName + ")"
                dict_data['ym'] = pageNum
            self.insertItem(tablename, dict_data)
            print(str(dict_data))
            # 如果有不定项选择题
            if d['examTypeName'] == "不定项选择题":
                for c in d['subQuestion']:
                    dict_data_c = dict()
                    dict_data_c['type'] = type  # 类别
                    dict_data_c['title'] = title  # 科目
                    dict_data_c['part'] = part  # 章
                    dict_data_c['part_class'] = part_class  # 节
                    dict_data_c['examId'] = d['examId']  # 上级id
                    dict_data_c['examId_c'] = c['examId']  # id
                    dict_data_c['examTypeName'] = c['examTypeName']  # 题型
                    dict_data_c['content'] = d['content']  # 题干
                    dict_data_c['subexamId'] = c['examId']  # id
                    dict_data_c['examTypeName'] = c['examTypeName']  # 题型
                    dict_data_c['returnQuestion'] = c['questionArr']['returnQuestion']  # 问题
                    dict_data_c['analysis'] = c['analysis']  # 分析
                    dict_data_c['answer'] = c['answer']  # 正确答案选项
                    # 选项
                    tag_count = 1
                    for t in c['optionList']:
                        dict_data_c['option_' + str(tag_count)] = t.replace("&nbsp;", "")
                        tag_count += 1
                    # 章+节
                    if len(c['knowledge']) > 0:
                        parentChapterName = c['knowledge'][0]['parentChapterName']  # 章
                        chapterName = c['knowledge'][0]['chapterName']  # 节
                        knowLedgeName = c['knowledge'][0]['knowLedgeName']  # 节
                        yaoQiuName = c['knowledge'][0]['yaoQiuName']  # 节
                        pageNum = c['knowledge'][0]['pageNum']  # 教材页码
                        dict_data_c['zsd'] = parentChapterName + ">" + chapterName + ">" + knowLedgeName + ">" + yaoQiuName
                        dict_data_c['ym'] = pageNum
                    self.insertItem(tablename, dict_data_c)
                    print(str(dict_data_c))

        # 翻页
        if cursor < page:
            print(f"============共【{page}】页，第【{cursor + 1}】页=============")
            self.get_part(cursor + 1, md5, tablename, type, title, part, part_class)

    def insertItem(self, tableName, data):
        my_set = db[tableName]
        my_set.insert_one(data)


if __name__ == '__main__':
    conn = MongoClient('127.0.0.1', 27017)
    db = conn["Test"]
    contensts = Contents()
    list_name = [
        '2020年8月证券从业资格考试《金融市场基础知识》考前点题卷二',
        '2020年8月证券从业资格考试《金融市场基础知识》考前点题卷一',

    ]
    list_md5 = [
        '931d9970e8e8b3cc8503dfcc2da22472',
        '68dff00e8b73eb107df50a499c3b5d08',

    ]
    for k, y in enumerate(list_name):
        part_class = y
        md5 = list_md5[k]
        # contensts.get_part(1, md5, "233网校_银行从业", "章节练习",
        #                "银行从业 - 中级法律法规与综合能力", "第三部分第一章 银行管理基础",
        #                 part_class)  # 章节练习 模拟试题  历年真题

        contensts.get_part(1, md5, "aa233网校_金融市场基础知识", "考前点题",
                           "证券从业及专项 - 金融市场基础知识", part_class,
                           "")  # 章节练习 模拟试题